package cz.cvut.kadledav.paa.knapsack;

import cz.cvut.kadledav.paa.knapsack.bo.ComputedInstanceSolution;
import cz.cvut.kadledav.paa.knapsack.bo.Instance;
import cz.cvut.kadledav.paa.knapsack.bo.InstanceCategorySolution;
import cz.cvut.kadledav.paa.knapsack.bo.InstanceSolution;
import cz.cvut.kadledav.paa.knapsack.tasks.ATask;
import cz.cvut.kadledav.paa.knapsack.util.FileUtil;
import cz.cvut.kadledav.paa.knapsack.util.KnapsackFileReadUtil;
import cz.cvut.kadledav.paa.knapsack.util.SolutionUtil;
import cz.cvut.kadledav.paa.knapsack.util.TaskUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: David Kadlecek
 * Date: 7.10.13
 */
public class Run {

   public static <T extends ATask>  InstanceCategorySolution doTaskForSize(int sN, Class<T> taskClazz, boolean assertResult, Map<String, Object> params) throws IOException {
        String path = "instances/knap_"+sN+".inst.dat";
        String pathSolutions = "solutions/knap_"+sN+".sol.dat";
        List<Instance> instances = KnapsackFileReadUtil.readInstances(path);

        List<ComputedInstanceSolution> computedSolutions = new ArrayList<>();
        for (Instance instance : instances) {
            computedSolutions.add(TaskUtil.measureTask(instance, taskClazz,params));
        }
        Map<Integer,InstanceSolution> solutionMap = KnapsackFileReadUtil.readSolutions(pathSolutions);

        InstanceCategorySolution solution = new InstanceCategorySolution();
        solution.setCategoryNumber(sN);
        solution.setAverageTime(SolutionUtil.computeAverageTime(computedSolutions));
        solution.setAvgRelativeDeviation(SolutionUtil.computeAvgRelativeDeviance(solutionMap, computedSolutions));
        solution.setMaxRelativeDeviation(SolutionUtil.computeMaxRelativeDeviance(solutionMap, computedSolutions));


        if(assertResult){
            assertCorrectness(computedSolutions, solutionMap);
        }
        return solution;
    }

    private static void assertCorrectness(List<ComputedInstanceSolution> computedSolutions, Map<Integer, InstanceSolution> solutionMap) {
        for (ComputedInstanceSolution computedSolution : computedSolutions) {
            InstanceSolution solution = solutionMap.get(computedSolution.getInstanceId());
            if(!computedSolution.isSameAsGiven(solution)){
                throw new RuntimeException("Error in compared solution for instance "+computedSolution.getInstanceId());
            }
        }
    }

    public static void doFor(Class<? extends ATask> taskClazz, int[] sizes, int times, boolean assertResult) throws IOException {
        doFor(taskClazz, sizes, times, assertResult, new HashMap());
    }

    public static void doFor(Class<? extends ATask> taskClazz, int[] sizes, int times, boolean assertResult, Map<String,Object> params) throws  IOException{

        long[] avgTimes = new long[sizes.length];
        double[] avgRelativeDeviations = new double[sizes.length];
        double[] maxRelativeDeviations = new double[sizes.length];
//        Map<String,Object>[] specialResults = new Map[sizes.length];
        int[] categoryNumber = new int[sizes.length];

        for (int j = 0; j < times; j++) {
            for (int i = 0; i < sizes.length; i++) {
                InstanceCategorySolution solution = Run.doTaskForSize(sizes[i], taskClazz, assertResult,params);
//                specialResults[i] = solution.getSpecialForTask();
                categoryNumber[i] = solution.getCategoryNumber();
                maxRelativeDeviations[i] = solution.getMaxRelativeDeviation();
                avgRelativeDeviations[i] += solution.getAvgRelativeDeviation();
                avgTimes[i] += solution.getAverageTime();

            }
        }

        List<InstanceCategorySolution> solutions = new ArrayList<>(sizes.length);
        for (int i = 0; i < sizes.length; i++) {
            InstanceCategorySolution solution = new InstanceCategorySolution();
            solution.setCategoryNumber(categoryNumber[i]);
            solution.setMaxRelativeDeviation(maxRelativeDeviations[i]);
            solution.setAvgRelativeDeviation(avgRelativeDeviations[i] / (double) times);
            solution.setAverageTime(Math.round(avgTimes[i] / (double) times));
//            solution.setSpecialForTask(specialResults[i]);
            solutions.add(solution);
        }
        FileUtil.write(taskClazz.getSimpleName() + "_" + times + "times_", solutions ,params);

    }
}
